#include "../head.h"

int main()
{
    printf("回射服务器starting\n");
    // 创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(6153);
    server_addr.sin_addr.s_addr = inet_addr("192.168.47.198");

    bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));

    listen(sockfd, 5);
    while (1)
    {
        struct sockaddr_in client_addr;
        memset(&client_addr, 0, sizeof(client_addr));
        int client_len = sizeof(client_addr);
        int connfd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len);
        printf("client ip: %s, port: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
        while (1)
        {

            char buf[1024];
            memset(buf, 0, sizeof(buf));
            ssize_t recv_res = recv(connfd, buf, sizeof(buf), 0);
            printf("recv_res: %s\n", buf);
            
            send(connfd, buf, sizeof(buf), 0);
            if (strcmp(buf, "exit") == 0||strcmp(buf, "#") == 0)
            {
                break;
            }
        }
        close(connfd);
        printf("client ip: %s exit\n", inet_ntoa(client_addr.sin_addr));
    }
    close(sockfd);
    printf("end\n");

    return 0;
}